//
//  XQNavigatorBar.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQNavigatorBar.h"

@interface XQNavigatorBar()
@property (nonatomic,strong) UIView *topView;
@property (nonatomic,strong) UIView *navBarView;
@property (nonatomic,strong) UIImageView *backImageView;
@property (nonatomic,strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation XQNavigatorBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.topView];
    [self.topView addSubview:self.navBarView];
    [self.navBarView addSubview:self.backImageView];
    [self.navBarView addSubview:self.titleLabel];
    [self.navBarView addSubview:self.backButton];
}

- (void)makeConstraints {
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.height.mas_equalTo([UIScreen mainScreen].topHeight);
    }];
    [self.navBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([UIScreen mainScreen].statusBarHeight);
        make.left.right.bottom.equalTo(@0);
    }];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerY;
        make.left.equalTo(@20);
        make.width.height.equalTo(@20);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.center;
    }];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(@0);
        make.width.equalTo(@60);
    }];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

- (void)setTitleColor:(UIColor *)titleColor {
    self.titleLabel.textColor = titleColor;
}

- (void)setTitleFont:(UIFont *)titleFont {
    self.titleLabel.font = titleFont;
}

- (void)setBackImageName:(NSString *)backImageName {
    self.backImageView.image = [UIImage imageNamed:backImageName];
}

- (void)setHiddenBack:(BOOL)hiddenBack {
    self.backImageView.hidden = hiddenBack;
    self.backButton.hidden = hiddenBack;
}

#pragma mark - private

- (void)backClick{
    if (self.backHandle) {
        self.backHandle();
    }
}

#pragma mark - lazy

- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = [UIColor clearColor];
    }
    return  _topView;
}

- (UIView *)navBarView {
    if (!_navBarView) {
        _navBarView = [[UIView alloc] init];
        _navBarView.backgroundColor = [UIColor clearColor];
    }
    return _navBarView;
}

- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [[UIImageView alloc] init];
        _backImageView.image = [UIImage imageNamed:@"left_icon"];
    }
    return _backImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:17];
        _titleLabel.textColor = [UIColor blackColor];
    }
    return _titleLabel;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

@end
